import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/view/address/address_administration_widget.dart';
import 'package:loxen/view/mine/mine_orders_widget.dart';
import 'package:loxen/view/mine/mine_staff_widget.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/login_view_model.dart';
import 'package:loxen/viewmode/mine_page_viewmodel.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'mine_cover_widget.dart';

class MinePageWidget extends StatefulWidget {
  const MinePageWidget({Key key}) : super(key: key);

  @override
  _MinePageWidgetState createState() => _MinePageWidgetState();
}

class _MinePageWidgetState extends State<MinePageWidget> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Builder(
      builder: (BuildContext context) {
        bool login = !Provider.of<LoginViewModel>(context, listen: false).isLogin();
        BaseState state = context.watch<MinePageViewModel>().mState;
        var staff = context.select<MinePageViewModel, bool>((value) => value.staff);
        if (state == BaseState.LOADING) {
          return LoadingWidget();
        }
        if (state == BaseState.FAIL) {
          return FailWidget(
            onClick: () {
              context.read<MinePageViewModel>().loadData();
            },
          );
        }
        if(login && staff) {
          return MineStaffWidget();
        }else{
          return Scaffold(
            backgroundColor: Colors.white,
            body: RefreshIndicator(
              onRefresh: () async {
                Provider.of<MinePageViewModel>(context, listen: false).loadData();
                return;
              },
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: ListView(
                  physics: ClampingScrollPhysics(),
                  children: <Widget>[
                    MineCover(),
                    Container(
                        color: Theme.of(context).backgroundColor,
                        padding: const EdgeInsets.only(top: 26, left: 18.5),
                        margin: const EdgeInsets.only(bottom: 6.5),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            _renderMenuItem(context,
                                iconPath: 'images/mine_study.png',
                                text: StringResources.wo_de_xue_xi,
                                route: Routes.mineStudys,
                                isLogin: login),
                            _renderMenuItem(context,
                                iconPath: 'images/mine_article.png',
                                text: StringResources.wo_de_zuo_pin,
                                route: Routes.mineWorks,
                                isLogin: login),
                            login
                                ? _renderMenuItem(context,
                                iconPath: 'images/mine_order.png',
                                text: StringResources.wo_de_ding_dan, onClick: () {
                                  Navigator.of(context)
                                      .push(MaterialPageRoute(builder: (_) => MineOrdersWidget()));
                                },
                                // route: Routes.mineOrders,
                                isLogin: login)
                                : SizedBox.shrink(),
                            login
                                ? _renderMenuItem(context,
                                arguments: {
                                  'showDistance': null,
                                },
                                iconPath: 'images/mine_addr.png',
                                text: StringResources.shou_yang_di_zhi, onClick: () {
                                  Navigator.of(context).push(MaterialPageRoute(
                                      builder: (_) => AddressAdministrationWidget(
                                        onClick: false,
                                      )));
                                }, isLogin: login)
                                : SizedBox.shrink(),
                            _renderMenuItem(context,
                                iconPath: 'images/mine_guanzhu.png',
                                text: StringResources.wo_de_guan_zhu,
                                route: Routes.mineFollower,
                                isLogin: login),
                            _renderMenuItem(context,
                                iconPath: 'images/mind_like.png',
                                text: StringResources.wo_zan_guo_de,
                                route: Routes.mineLike,
                                isLogin: login),
                            _renderMenuItem(context,
                                iconPath: 'images/mine_comment.png',
                                text: StringResources.wo_de_ping_lun,
                                route: Routes.mineComment,
                                isLogin: login),
                            _renderMenuItem(context,
                                iconPath: 'images/mine_settings1.png',
                                text: StringResources.wo_de_she_zhi,
                                route: Routes.mineSettings,
                                isLogin: true),
                          ],
                        )),
                  ],
                ),
              ),
            ),
          );
        }
      },
    );
  }

  Widget _renderMenuItem(BuildContext context,
      {Object arguments,
      String iconPath,
      String text,
      bool hasRemind = false,
      String route,
      loginOut = false,
      needToken = true,
      bool isLogin,
      Function() onClick}) {
    return GestureDetector(
      onTap: () {
        if (onClick != null) {
          onClick.call();
          return;
        }
        if (isLogin && route != null) {
          Navigator.of(context).pushNamed(route, arguments: arguments);
        } else {
          Navigator.of(context).pushNamed(Routes.login_page);
        }
      },
      child: Container(
        padding: const EdgeInsets.only(top: 14, bottom: 14),
        decoration: BoxDecoration(
            border: Border(
          bottom: BorderSide(color: Colors.white, width: 0.5),
        )),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  iconPath,
                  width: 50.w,
                  height: 50.w,
                  color: isLogin ? Color(0xFF000000) : Colors.black54,
                ),
                Container(
                  margin: EdgeInsets.only(left: 28.w),
                  child: Text(
                    text,
                    style: TextStyle(
                      color: isLogin ? Color(0xFF000000) : Colors.black54,
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ],
            ),
            Container(
              padding: EdgeInsets.only(right: 22.w),
              child: Row(
                children: <Widget>[
                  hasRemind
                      ? Container(
                          width: 15.w,
                          height: 15.w,
                          margin: EdgeInsets.only(right: 33.w),
                          decoration: BoxDecoration(
                            color: Color(0xFF0CB7D9),
                            borderRadius: BorderRadius.circular(16.w),
                          ))
                      : SizedBox.shrink(),
                  Image.asset('images/mine_right.png', width: 30.w),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
